package com.esm.app;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.esm.tool.Config;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyApplication extends Application {
    private static final List<Activity> mList = new LinkedList<Activity>();
    /**
     * test 01
     **/
    public static String SERVERPATH = Config.SERVER_PATH;
    public static String qx;
    public static String yy;
    public static int index = -2;
    public static List<Activity> mylist = new LinkedList<Activity>();
    public static File cacheDir;
    public static List<String> slist = new ArrayList<String>();
    public static Context applicationContext;
    public static MyApplication instance;
    public static String liveName = "";
    public static boolean isLogin = false;
    public static String path = "";
    public static int width, height;
    public static long xtime = 0; // 一体机时间 - 本机时间    秒     差距<60秒 不用设置
    public static String registrationId = "";
    public static String packagename = "esm";
    public static String rec = "";
    static SharedPreferences sharedUserInfo;
    static SharedPreferences serversInfo;
    private static MyApplication mInstance = null;
    private static RequestQueue mRequestQueue;
    private static Typeface typeFaceLight;
    // login user name
    public final String PREF_USERNAME = "username";
    private String ip = "";
    private String port = "";

    public static RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getInstance());
        }
        return mRequestQueue;
    }

    public static boolean isIpv4(String ipAddress) {
        String ip = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
        Pattern pattern = Pattern.compile(ip);
        Matcher matcher = pattern.matcher(ipAddress);
        return matcher.matches();
    }

    public static Typeface getLightTypeFace() {
        if (typeFaceLight == null) {
            typeFaceLight = Typeface.createFromAsset(getInstance().getAssets(), "fonts/light.ttf");
        }
        return typeFaceLight;
    }

    public static MyApplication getInstance() {
        return mInstance;
    }

    public static void exit() {
        try {
            for (Activity activity : mList) {
                if (activity != null)
                    activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }

    public static SharedPreferences getSharedUserInfo() {
        if (sharedUserInfo == null) {
            sharedUserInfo = getInstance().getSharedPreferences("test", Context.MODE_PRIVATE);
        }
        return sharedUserInfo;
    }

    public static SharedPreferences getServersInfo() {
        if (serversInfo == null) {
            serversInfo = getInstance().getSharedPreferences("servers", Context.MODE_PRIVATE);
        }
        return serversInfo;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        ip = MyApplication.getServersInfo().getString("serverip", "");
        port = MyApplication.getServersInfo().getString("serverport", "");
        qx = MyApplication.getSharedUserInfo().getString("qx", "");
        MyApplication.SERVERPATH = "http://" + ip + ":" + port;
        applicationContext = this;
        instance = this;
        yy = Locale.getDefault().getLanguage();
        yy = MyApplication.getSharedUserInfo().getString("language", yy);
    }

    // add Activity
    public void addActivity(Activity activity) {
        mList.add(activity);
    }

    public void onLowMemory() {
        super.onLowMemory();
        System.gc();
    }

}
